package com.lei.androidheight_four;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lei.androidheight_four.contract.LoginContract;
import com.lei.androidheight_four.model.LoginModel;
import com.lei.androidheight_four.presenter.LoginPresenter;
import com.lei.mvpmodel.view.BaseActivty;

public class MainActivity extends BaseActivty<LoginPresenter> implements LoginContract.View {
    private LoginPresenter loginPresenter;
    private EditText etLoginName;
    private EditText etLoginPwd;
    private EditText etLoginRepwd;
    private Button btRegister;
    private Button btLogin;
    private Button btReg;
    private Button btLog;

    @Override
    public void gomain(String msg) {
        Toast.makeText(this, ""+msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public int bindlayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initview() {
        etLoginName = (EditText) findViewById(R.id.et_login_name);
        etLoginPwd = (EditText) findViewById(R.id.et_login_pwd);
        etLoginRepwd = (EditText) findViewById(R.id.et_login_repwd);
        btRegister = (Button) findViewById(R.id.bt_register);
        btLogin = (Button) findViewById(R.id.bt_login);
        btReg = (Button) findViewById(R.id.bt_reg);
        btLog = (Button) findViewById(R.id.bt_log);

        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = etLoginName.getText().toString().trim();
                String pwd = etLoginPwd.getText().toString().trim();
                if (name.length()>3){
                    if (pwd.length()>3){
                        loginPresenter.register(name,pwd);
                    }else {
                        etLoginPwd.setError("密码的长度需大于三位");
                    }
                }else {
                    etLoginName.setError("用户名的长度需大于三位");
                }
                loginPresenter.login(name,pwd);
            }
        });
        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etLoginRepwd.setVisibility(View.VISIBLE);
            }
        });
        btLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etLoginRepwd.setVisibility(View.GONE);
            }
        });
        btReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = etLoginName.getText().toString().trim();
                String pwd = etLoginPwd.getText().toString().trim();
                if (name.length()>3){
                    if (pwd.length()>3){
                        loginPresenter.register(name,pwd);
                    }else {
                        etLoginPwd.setError("密码的长度需大于三位");
                    }
                }else {
                    etLoginName.setError("用户名的长度需大于三位");
                }
            }
        });
    }

    @Override
    public void inject() {
        loginPresenter = new LoginPresenter(new LoginModel(),this);
    }
}